(function(jQuery){
	$(function(){

		$(".topheader").load("header.html",function(){
            function getCount(obj){
                if(sessionStorage.getItem("userInfo")==null){
                    let cookieList=JSON.parse($.cookie("carts")||'[]');

                    if(cookieList.length==0){
                        obj.html(0);
                    }else{
                        let count=0;
                        cookieList.forEach((ele,index)=>{
                            count+=ele.num;
                        })
                        obj.text(count);
                    }
                }else{
                    let oUser=JSON.parse(sessionStorage.getItem("userInfo"));
                    $.ajax({
                        url:"../../server/getCartById.php",
                        type:"post",

                        data:{u_id:Number(oUser.u_id)}
                    }).then(function (res) {

                        if(res.length>=1){
                            let count=0;
                            res.forEach((ele,index)=>{
                                count+=ele.c_num;
                            })

                            obj.text(count);
                        }else{
                            obj.html(0);
                        }
                    })
                }
            }
                $(function () {

                        getCount($(".topright .count"));


                })
        })
		$(".footer").load("footer.html",function(){})
		$(".nav").load("nav.html",function(){})
		$(".bar").load("bar.html",function(){
            function getCount(obj){
                if(sessionStorage.getItem("userInfo")==null){
                    let cookieList=JSON.parse($.cookie("carts")||'[]');

                    if(cookieList.length==0){
                        obj.html(0);
                    }else{
                        let count=0;
                        cookieList.forEach((ele,index)=>{
                            count+=ele.num;
                        })
                        obj.text(count);
                    }
                }else{
                    let oUser=JSON.parse(sessionStorage.getItem("userInfo"));
                    $.ajax({
                        url:"../../server/getCartById.php",
                        type:"post",

                        data:{u_id:Number(oUser.u_id)}
                    }).then(function (res) {

                        if(res.length>=1){
                            let count=0;
                            res.forEach((ele,index)=>{
                                count+=ele.c_num;
                            })

                            obj.text(count);
                        }else{
                            obj.html(0);
                        }
                    })
                }
            }
            $(function () {

                    getCount($(".rightbar .count"));

                    $(".owner").parent("li").click(function () {
                        if(sessionStorage.getItem("userInfo")==null){
                            location="login.html";
                        }else{
                            if($(this).parents(".rightbar").css("right")!=0){
                                $(this).parents(".rightbar").animate({right:0});

                            }
                            if($(this).parents(".rightbar").css("right")=="0px"){
                                $(this).parents(".rightbar").animate({right:"-360px"});
                                console.log(456);
                            }
                            let oUser=JSON.parse(sessionStorage.getItem("userInfo"));
                            let str=`<dl>
                                    <dt>ID:</dt>
                                    <dd>${oUser.u_id}</dd>
                                    <dt>您的用户名:</dt>
                                    <dd>${oUser.u_name}</dd>
                                    <dt>您的邮箱:</dt>
                                    <dd>${oUser.u_email}</dd>
                                    <a href="javascript:;;" class="exit">退出登录</a>
		                        </dl>`;
                            $(".rightbar .content").html(str);
                        }

                    })
                    $(".rightbar").on("click",".exit",function () {
                        if(confirm("您确定要退出此账号吗？")){
                            sessionStorage.clear();
                            location.reload();
                        }
                    })
            })
		})
		

		let $oImg=$(".leftbar li img");
		$(".leftbar li").hover(function(){
			let index=$(this).index();
			
			$oImg.eq(index).attr("src","/butaonet/client/src/img/base/zsa"+(index+1)+".jpg");
		},function(){
			let index=$(this).index();
			
			$oImg.eq(index).attr("src","/butaonet/client/src/img/base/cfd"+(index+1)+".jpg");
		})
		

		$(".leftbar li:last").click(function(){
			$("html,body").animate({
				scrollTop:0
			},1000)
		})


    })

})(jQuery)
